package com.senmiao.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.senmiao.ssm.entity.Admin;
import com.senmiao.ssm.entity.Permission;
import com.senmiao.ssm.mapper.AdminMapper;
import com.senmiao.ssm.mapper.PermissionMapper;
import com.senmiao.ssm.service.RealmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RealmServiceImpl implements RealmService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Admin getAdminByAccount(String account) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("account", account));
    }

    @Override
    public Set<String> getPermissionsOfAdminByAdminId(Integer adminId) {
        List<Permission> permissions = permissionMapper.listByAdminId(adminId);
        HashSet<String> permissionStrings = new HashSet<>();
        permissions.forEach(e->permissionStrings.add(e.getPath()+":"+e.getMethod()));
        return permissionStrings;
    }
}
